﻿using SnmpForm.DbWrapper.DbDataTypes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SnmpForm.FaultDetect
{
    class DetectData
    {
        //使用单件模式
        static DetectData _instance = null;
        public static DetectData Instance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new DetectData();
                }
                return _instance;
            }
        }

        Dictionary<int, HashSet<int>> mapFaultAp = new Dictionary<int, HashSet<int>>();

        public bool ModifyWithEvent(FaultDetectEvent ev)
        {
            
            bool bModify = false;
            HashSet<int> setFault = null;
            bool bExist = mapFaultAp.TryGetValue(ev.apid, out setFault);
            FailureApInfo failureAP = null;
            if (!bExist)
            {
                setFault = new HashSet<int>();
                mapFaultAp[ev.apid] = setFault;
            }

            if (ev.repaired)
            {
                /*if (setFault.Contains(ev.faultid))
                {
                    setFault.Remove(ev.faultid);
                 *  故障修复入库
                 *  failureAP = new FailureApInfo(ev.apid,ev.faultid,"故障修复")
                 *  BusinessWrapper.User.Instance.InsertFaultAP(failureAP);
                    bModify = true;
                }*/
                if (setFault.Count > 0)
                {
                    failureAP = new FailureApInfo(ev.apid, ev.faultid, "故障修复");
                    BusinessWrapper.User.Instance.InsertFaultAP(failureAP);
                    setFault.Clear();
                    bModify = true;
                }
            }
            else
            {
                if (setFault.Add(ev.faultid))
                {
                    //故障入库
                    failureAP = new FailureApInfo(ev.apid, ev.faultid, null);
                    BusinessWrapper.User.Instance.InsertFaultAP(failureAP);
                    bModify = true;
                }
            }
            return bModify;
        }

        public Dictionary<int, HashSet<int>> Get()
        {
            return mapFaultAp;
        }

        public bool IsFault(int apid)
        {
            bool bFault = false;
            HashSet<int> setFault = null;
            bool bExist = 
                mapFaultAp.TryGetValue(apid, out setFault);
            if (bExist)
            {
                if (setFault.Count>0)
                {
                    bFault = true;
                }
            }
            return bFault;
        }
    }
}
